package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.HProjectInventoryMark;
import com.zhongwang.mms.gen.entity.PInventory;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HProjectInventoryMarkDao;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.model.HProjectInventoryMarkModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:项目划库存标记
 * @author: th
 * @time: 2020/5/12 9:39
 */
@Service
@Log4j2
public class ProToInvMarkService {

    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HProjectInventoryMarkDao hProjectInventoryMarkDao;
    @Autowired
    private LogService logService;

    public List<HInventoryModel> getInventoryList(HInventoryQueryModel query) {
        List<HInventoryModel> list = null;
        try {
            list = hInventoryDao.findProToInvInventoryListByQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 标记确定
     *
     * @param hProjectInventoryMarkModel
     * @return
     */
    @Transactional
    public JsonResult applyMark(HProjectInventoryMarkModel hProjectInventoryMarkModel) {

        JsonResult jsonResult = new JsonResult();
        try {
            HInventory hInventory = hInventoryDao.selectAllById(hProjectInventoryMarkModel.getPimBeforeItyId());
            if (hInventory.getItyAvaNum() < hProjectInventoryMarkModel.getPimNum()) {
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("当前选取的库存中物料名称为：【" + hInventory.getItyMatName() + "】的物料因可用数量不足，而导致此次标记失败！");
                return jsonResult;
            }
            hProjectInventoryMarkModel.setPimProposerId(ShiroUtils.getUser().getId());
            hProjectInventoryMarkModel.setPimDate(new Date());
            hProjectInventoryMarkModel.setPimMatCode(hProjectInventoryMarkModel.getPimSapCode());
            hProjectInventoryMarkModel.setPimAppStatus(1);
            hProjectInventoryMarkDao.insertSelective(hProjectInventoryMarkModel);
            PInventory changeItyAnaNumModel = new PInventory();
            changeItyAnaNumModel.setItyId(hInventory.getItyId());
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() - hProjectInventoryMarkModel.getPimNum());
            if (hProjectInventoryMarkModel.getPimWeight() != null && hProjectInventoryMarkModel.getPimWeight() != 0) {
                hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() - hProjectInventoryMarkModel.getPimWeight());
            }
            hInventoryDao.updateByPrimaryKeySelective(hInventory);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("标记成功！");
            //logService.saveLog("【外购件】项目划库存", "标记成功，被标记的物料名称是：" + param.getPimMatDesc() + "，所属的项目的项目编号是：" + param.getPimBeforeProCode() + ",标记数量为：" + param.getPimNum());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("标记失败！");
        }
        return jsonResult;
    }

    /**
     * 项目划库存申请页面数据
     *
     * @param query
     * @return
     */
    public List<HProjectInventoryMarkModel> applyList(HProjectInventoryMarkModel query) {
        return hProjectInventoryMarkDao.selectAllByQuery(query);
    }

    /**
     * 获取标记
     *
     * @param hProjectInventoryMarkModel
     * @return
     */
    public JsonResult getHProjectInventoryMark(HProjectInventoryMarkModel hProjectInventoryMarkModel) {
        JsonResult jsonResult = new JsonResult();
        try {
            HProjectInventoryMarkModel hProjectInventoryMarkModel1 = hProjectInventoryMarkDao.selectProInvByPrimaryKey(hProjectInventoryMarkModel.getPimId());
            HInventory hInventory = hInventoryDao.selectAllById(hProjectInventoryMarkModel1.getPimBeforeItyId());
            hProjectInventoryMarkModel1.setPimBeforeAvaNum(hProjectInventoryMarkModel1.getPimNum() + hInventory.getItyAvaNum());
            if (hInventory.getItyAvaWeight() != null && hInventory.getItyAvaWeight() != 0) {
                hProjectInventoryMarkModel1.setPimBeforeAvaWeight(hProjectInventoryMarkModel1.getPimWeight() + hInventory.getItyAvaWeight());
            }
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setData(hProjectInventoryMarkModel1);
            jsonResult.setMsg("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("获取失败！");
        }
        return jsonResult;
    }

    /**
     * 修改标记保存
     *
     * @param hProjectInventoryMarkModel
     * @return
     */
    @Transactional
    public JsonResult saveMark(HProjectInventoryMarkModel hProjectInventoryMarkModel) {
        JsonResult jsonResult = new JsonResult();
        try {
            HProjectInventoryMarkModel hProjectInventoryMarkModel1 = hProjectInventoryMarkDao.selectProInvByPrimaryKey(hProjectInventoryMarkModel.getPimId());
            HInventory hInventory = hInventoryDao.selectAllById(hProjectInventoryMarkModel.getPimBeforeItyId());
            if ((hInventory.getItyAvaNum() + hProjectInventoryMarkModel1.getPimNum()) < hProjectInventoryMarkModel.getPimNum()) {
                jsonResult.setType(JsonResult.ResultType.ERROR);
                jsonResult.setMsg("当前选取的库存中物料名称为：【" + hInventory.getItyMatName() + "】的物料因可用数量不足，而导致此次标记失败！");
                return jsonResult;
            }
            HProjectInventoryMark updateHProjectInventoryMark = new HProjectInventoryMark();
            updateHProjectInventoryMark.setPimId(hProjectInventoryMarkModel.getPimId());
            updateHProjectInventoryMark.setPimNum(hProjectInventoryMarkModel.getPimNum());
            updateHProjectInventoryMark.setPimWeight(hProjectInventoryMarkModel.getPimWeight());
            updateHProjectInventoryMark.setPimRemark(hProjectInventoryMarkModel.getPimRemark());
            if (hProjectInventoryMarkModel.getPimAppStatus().equals(2)) {
                updateHProjectInventoryMark.setPimAppStatus(1);
            }
            hProjectInventoryMarkDao.updateByPrimaryKeySelective(updateHProjectInventoryMark);
            if (updateHProjectInventoryMark.getPimWeight() != null && updateHProjectInventoryMark.getPimWeight() != 0) {
                hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() + hProjectInventoryMarkModel1.getPimWeight() - updateHProjectInventoryMark.getPimWeight());
            }
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() + hProjectInventoryMarkModel1.getPimNum() - updateHProjectInventoryMark.getPimNum());
            hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("标记成功！");
            //logService.saveLog("【外购件】项目划库存", "标记成功，被标记的物料名称是：" + param.getPimMatDesc() + "，所属的项目的项目编号是：" + param.getPimBeforeProCode() + ",标记数量为：" + param.getPimNum());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("标记失败！");
        }
        return jsonResult;
    }

    /**
     * 删除标记
     *
     * @param hProjectInventoryMarkModel
     * @return
     */
    @Transactional
    public JsonResult removeMark(HProjectInventoryMarkModel hProjectInventoryMarkModel) {

        JsonResult jsonResult = new JsonResult();
        try {
            List<HProjectInventoryMarkModel> hProjectInventoryMarkModels = hProjectInventoryMarkDao.findPimAppStatusByPrimaryKeys(hProjectInventoryMarkModel.getPimIds());
            for (HProjectInventoryMarkModel hProjectInventoryMarkModel1 : hProjectInventoryMarkModels) {
                HInventory hInventory = hInventoryDao.selectAllById(hProjectInventoryMarkModel1.getPimBeforeItyId());
                HInventory changeItyAnaNumModel = new HInventory();
                changeItyAnaNumModel.setItyId(hProjectInventoryMarkModel1.getPimBeforeItyId());
                hInventory.setItyAvaNum(hInventory.getItyAvaNum() + hProjectInventoryMarkModel1.getPimNum());
                if (hProjectInventoryMarkModel1.getPimWeight() != null && hProjectInventoryMarkModel1.getPimWeight() != 0) {
                    hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() + hProjectInventoryMarkModel1.getPimWeight());
                }
                hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            }
            hProjectInventoryMarkDao.deleteByPrimaryKeys(hProjectInventoryMarkModel);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("删除标记成功！");
            //logService.saveLog("【外购件】项目划库存", "删除标记");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("删除标记失败！");
        }
        return jsonResult;
    }

    /**
     * 提交申请
     *
     * @param hProjectInventoryMarkModel
     * @return
     */
    @Transactional
    public JsonResult submit(HProjectInventoryMarkModel hProjectInventoryMarkModel) {
        JsonResult jsonResult = new JsonResult();
        try {
            hProjectInventoryMarkModel.setPimAppStatus(3);
            hProjectInventoryMarkDao.updatePimAppStatusByPrimaryKeys(hProjectInventoryMarkModel);
            jsonResult.setType(JsonResult.ResultType.SUCCESS);
            jsonResult.setMsg("提交成功！");
            logService.saveLog("【成品库】项目划库存", "提交申请！");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            jsonResult.setType(JsonResult.ResultType.ERROR);
            jsonResult.setMsg("提交失败！");
        }
        return jsonResult;
    }
}
